﻿<?
function getCatIdByName($cat_rewrite){
	$db_select = new db_query("SELECT cat_id FROM categories_multi
										WHERE cat_rewrite = '" . replaceMQ($cat_rewrite) . "' 
										LIMIT 1");
	if($row = mysql_fetch_assoc($db_select->result)){
		return $row["cat_id"];
	}else{
		saveLog1("getCatIdByName", $cat_rewrite);
		return 0;
	}
}
function getTeamIdByName($tea_rewrite, $tea_name = "", $cat_id = 0){
	$cat_id = intval($cat_id);
	$db_select = new db_query("SELECT tea_id FROM teams
										WHERE tea_rewrite = '" . replaceMQ($tea_rewrite) . "' 
										LIMIT 1");
	if($row = mysql_fetch_assoc($db_select->result)){
		return $row["tea_id"];
	}else{
		//neu truyen vao thong tin day du thi insert them
		if($tea_name != '' && $cat_id != 0){
			$db_ex = new db_execute_return();
			$last_id = $db_ex->db_execute("INSERT IGNORE INTO teams(tea_name,tea_rewrite,tea_cat_id)
													 VALUES('" . replaceMQ($tea_name) . "','" . replaceMQ($tea_rewrite) . "'," . $cat_id . ")");
		 	return $last_id;
		}
		saveLog1("getTeamIdByName", $tea_rewrite);
		return 0;
	}
}
function getDomainIdByName($domain){
	$db_select = new db_query("SELECT dom_id FROM domains
										WHERE dom_name = '" . replaceMQ($domain) . "' 
										LIMIT 1");
	if($row = mysql_fetch_assoc($db_select->result)){
		return $row["dom_id"];
	}else{
		//neu truyen vao thong tin day du thi insert them
		$db_ex = new db_execute_return();
		$last_id = $db_ex->db_execute("INSERT IGNORE INTO domains(dom_name)
												 VALUES('" . replaceMQ($domain) . "')");
	 	saveLog1("getDomainIdByName", $domain);
	 	return $last_id;
	}
}
function getPlaeyrIdByName($name = "", $tea_id = 0){
	$tea_id 	= intval($tea_id);
	$name		= str_replace(' PEN','',$name);
	$db_select = new db_query("SELECT pla_id FROM players
										WHERE pla_name = '" . replaceMQ($name) . "' 
										LIMIT 1");
	if($row = mysql_fetch_assoc($db_select->result)){
		return $row["pla_id"];
	}else{
		//neu truyen vao thong tin day du thi insert them
		if($name != '' && $tea_id != 0){
			$db_ex = new db_execute_return();
			$last_id = $db_ex->db_execute("INSERT IGNORE INTO players(pla_name,pla_tea_id)
													 VALUES('" . replaceMQ($name) . "'," . $tea_id . ")");
		 	return $last_id;
		}
		saveLog1("getPlaeyrIdByName", $name);
		return 0;
	}
}
function generate_name1($filename, $prefix=""){
	$name = "";
	for($i=0; $i<3; $i++){
		$name .= chr(rand(97,122));
	}
	$today= getdate();
	if($prefix == "") $name .= $today[0];
	else $name = $prefix . $name . $today[0];
	$ext	= substr($filename, (strrpos($filename, ".") + 1));
	return $name . "." . $ext;
}
function getPictureNameByUrl($url){
	$matp_md5_url = md5($url);
	$db_select = new db_query("SELECT matp_filename FROM match_pictemp
										WHERE matp_md5_url = '" . $matp_md5_url . "' 
										LIMIT 1");
	if($row = mysql_fetch_assoc($db_select->result)){
		return $row["matp_filename"];
	}else{
		$matp_filename = generate_name1($url);
		//neu truyen vao thong tin day du thi insert them
		$db_ex = new db_execute("INSERT IGNORE INTO match_pictemp(matp_filename,matp_md5_url,matp_url)
													 VALUES('" . $matp_filename . "','" . $matp_md5_url . "','" . replaceMQ($url) . "')");
		saveLog1("getPictureNameByUrl", $matp_filename);
		return $matp_filename;
	}
}
//function lấy khối html
function get_url_html($url)
{
	$result 	= curl_get_content($url);
	$html 	= str_get_html($result); 
	unset($result);
	return $html;
}

function curl_get_content($url)
{
	$ini = curl_init($url);
	curl_setopt($ini, CURLOPT_HEADER, false);
	$userAgent  = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
	curl_setopt($ini, CURLOPT_USERAGENT, $userAgent);
	curl_setopt($ini, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ini, CURLOPT_REFERER, 'http://vatgia.com');
	$result 	= curl_exec($ini);
	//echo $userAgent;
	unset($ini);
	
	return $result;
}

function get_list_link($row){
	$foc_url			=	$row["foc_url"];
	$html				=	get_url_html($foc_url);	
	
	//nếu nhóm html khác rỗng thì lấy nhóm trước rồi lấy  phần tử con trong nhóm sau
	$for_group		=	isset($row["for_group"]) ? trim($row["for_group"]) : "";
	$arrayReturn	=	array();
	
	if($for_group != ""){
		$group				=	$html->find($row["for_group"]);
		$i	=	0;
		$j	=	0;
		$k	=	0;
		foreach($group as $tr){
			//echo $list->plaintext . '<br>';			
			if($row["for_luat_link"] != ""){
				$list				=	$tr->find($row["for_luat_link"], 0);
				$url				=	trim($row["for_site_name"]) . @$list->href;
				$title			=	@$list->plaintext;
				if(strval($url) == "" || strval($title) == ""){
					continue;
				}
				$arrayReturn[$i]["url"] 	= $url;
				$arrayReturn[$i]["title"] 	= $title;
			}
			if($row["for_city"] != ""){
				$arrayReturn[$i]["city"]	=	$tr->find($row["for_city"], 0)->plaintext;
			}else{
				$arrayReturn[$i]["city"]	=	$row["for_default_city"];
			}
			if($row["for_picture"] != ""){
				$arrayReturn[$i]["picture"]	=	$tr->find($row["for_picture"], 0)->src;
				if($arrayReturn[$i]["picture"] == "style/images/spacer.gif"){
					$arrayReturn[$i]["picture"] = "";
				}
			}
			if($row["for_date"] != ""){
				$date								=	@$tr->find($row["for_date"], 0)->plaintext;
				$date								=	preg_replace("/[^0-9\/\-]/i","",$date);
				$date								=	str_replace("-", "/", $date);
				$date								=	explode("/", $date);
				$date[2]							=	isset($date[2]) ? $date[2] : 2011;
				if(strlen($date[2]) == 2){
					$date[2]						=	"20" . $date[2];
				}
				$date	= implode("/", $date);
				$date	= convertDateTime1($date, "0:0:0");
				$arrayReturn[$i]["date"]	=	$date;
			}
			/*
			//lay teaser
			if(isset($row["for_teaser"])){
				$list				=	$tr->find($row["for_teaser"]);
				foreach($list as $e){
					$teaser		=	$e->innertext();
					$arrayReturn[$j]["teaser"] 	= $teaser;
					$j++;
				}
			}
			//*/
			$i++;
		}
		
	}else{
		$list				=	$html->find($row["for_luat_link"]);
		$i					=	0;
		foreach($list as $e){
			$url			=	trim($row["for_site_name"]) . $e->href;
			$title		=	$e->plaintext;
			$arrayReturn[$i] = array("url" => $url, "title" => $title);
			$i++;
		}
	
	}
	$html->clear();
	unset($html);
	unset($list);
	return $arrayReturn;
}


function get_detail_data($row){
	$row["dau_url"] = str_replace("http://rongbay.com//", "http://rongbay.com/", $row["dau_url"]);
	$html				=	get_url_html($row["dau_url"]);
	
	$arrayReturn	=	array();
	//echo $row["for_luat_detail_desc"];
	if($html->find($row["for_luat_detail_desc"]) != null){
		$index	=	0;
		if(strpos($row["dau_url"], "muabanoto.vn") !== false){
			$index = 1;
		}
		$arrayReturn["description"]	= $html->find($row["for_luat_detail_desc"], $index)->innertext();
	}else{
		$arrayReturn["description"]	= "";
	}
	$for_need						=	isset($row["for_need"]) ? $row["for_need"] : "";
	$arrayReturn["title"]		= ($html->find($row["for_luat_detail_title"]) != null) ? $html->find($row["for_luat_detail_title"], 0)->plaintext : "";
	$for_picture					=	isset($row["for_picture"]) ? $row["for_picture"] : "";
	if($for_picture != ""){
		$arrayReturn["image"]	= ($html->find($row["for_picture"]) == null) ? "" : $html->find($row["for_picture"], 0)->src;
	}
	$for_default_need				=	isset($row["for_default_need"]) ? $row["for_default_need"] : "";
	if($for_need != ""){
		$arrayReturn["need"]	= ($html->find($row["for_need"]) == null) ? "" : $html->find($row["for_need"], 0)->plaintext;
		//echo $arrayReturn["need"];
	}else{
		if($for_default_need != ""){
			$arrayReturn["need"]	= $for_default_need;
		}
	}
	if(strpos($row["dau_url"], "muabanoto.vn") !== false){
		$for_date				=	isset($row["for_date"]) ? $row["for_date"] : "";
		$for_date_index		=	isset($row["for_date_p"]) ? intval($row["for_date_p"]) : 0;
		if($for_date != ""){
			$arrayReturn["date"]	= ($html->find($row["for_date"]) == null) ? "" : $html->find($row["for_date"], $for_date_index)->plaintext;
			$arrayReturn["date"]	= preg_replace("/[^0-9\/\-]/i","",$arrayReturn["date"]);
		}
	}
	if(strpos($row["dau_url"], "muaban.net") !== false){
		foreach($html->find("tr") as $tr){
			
			if(strpos($tr->find("td", 0)->plaintext, "Ngày đăng") !== false){
				$arrayReturn["date"]	= $tr->find("td", 2)->plaintext;
				$arrayReturn["date"]	= preg_replace("/[^0-9\/]/i","",$arrayReturn["date"]);
			}			
		}
		$arrayReturn["city"] = isset($row["for_default_city"]) ? $row["for_default_city"] : "";
	}
	$for_email						=	isset($row["for_email"]) ? $row["for_email"] : "";
	if($for_email != ""){
		//echo $for_email;
		$arrayReturn["email"]	= ($html->find($row["for_email"]) == null) ? "" : $html->find($row["for_email"], 0)->plaintext;
		//echo $arrayReturn["need"];
	}
	return $arrayReturn;
	
}

function convertDateTime1($strDate = "", $strTime = ""){
	//Break string and create array date time
	$array_replace	= array("/", ":");
	$strDate		= str_replace($array_replace, "-", $strDate);
	$strTime		= str_replace($array_replace, "-", $strTime);
	$strDateArray	= explode("-", $strDate);
	$strTimeArray	= explode("-", $strTime);
	$countDateArr	= count($strDateArray);
	$countTimeArr	= count($strTimeArray);
	
	//Get Current date time
	$today			= getdate();
	$day				= $today["mday"];
	$mon				= $today["mon"];
	$year				= $today["year"];
	$hour				= $today["hours"];
	$min				= $today["minutes"];
	$sec				= $today["seconds"];
	//Get date array
	switch($countDateArr){
		case 2:
			if(intval($strDateArray[1]) > 12)
			{
				$day	= intval($strDateArray[1]);
				$mon	= intval($strDateArray[0]);
			}else
			{
				$day		= intval($strDateArray[0]);
				$mon		= intval($strDateArray[1]);
			}
			break;
			
		case $countDateArr >= 3:
			if(intval($strDateArray[1]) > 12)
			{
				$day		= intval($strDateArray[1]);
				$mon		= intval($strDateArray[0]);
				$year 		= intval($strDateArray[2]);
			}else
			{
				$day		= intval($strDateArray[0]);
				$mon		= intval($strDateArray[1]);
				$year 		= intval($strDateArray[2]);
			}
			break;
	}
	//Get time array
	switch($countTimeArr){
		case 2:
			$hour		= intval($strTimeArray[0]);
			$min		= intval($strTimeArray[1]);
			break;
		case $countTimeArr >= 3:
			$hour		= intval($strTimeArray[0]);
			$min		= intval($strTimeArray[1]);
			$sec		= intval($strTimeArray[2]);
			break;
	}
	//Return date time integer
	if(@mktime($hour, $min, $sec, $mon, $day, $year) == -1) return $today[0];
	else return mktime($hour, $min, $sec, $mon, $day, $year);
}
function get_google_counting($domain)
{
    $url = "http://www.google.com.vn/search?q=" . urlencode("site:" . $domain) . "&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a";
    $html = get_url_html($url);
   
    $counting = $html->find("div[id=resultStats]", 0);
    $numberCounted = $counting->plaintext;
    
    $start = strpos($numberCounted, " ");
    $end = strpos($numberCounted, " ", 9);
    $lenghtEnought = $end - $start;
    $result = substr($numberCounted, $start, $lenghtEnought);
    
    if($result == false)
        return '0';
    else
        return $result;
}

function get_category_indb($site_name, $url, $luat = "", $add_path = ''){
	
	$url = str_replace("http://","",$url);
	$url = "http://" . $url;
	
	$html 			= get_url_html($url);
	$container 		= $html->find($luat);
	$array_return 	= array();
	foreach($container as $each)
	{
		$link = $each->href;
		if(strpos($site_name, "muaban.net") !== false){
			$link					=	$add_path . $link;
		}
		
		$link						=	break_url_forum(check_url($site_name, $link));
		if(strpos($link, "vnexpress.net") !== false){
			$link					=	$link . $add_path;
		}
		
		$array_return[]		=	array("title" => $each->plaintext,"link" => $link);
	}
	unset($html);
	return $array_return;
}

function get_link_data($site_url, $url, $luat = ""){
	$url = str_replace("http://","",$url);
	$url = "http://" . $url;
	//echo "|" . $luat . "|";
	$html 			= get_url_html($url);
	$container 		= $html->find($luat);
	$array_return 	= array();
	foreach($container as $each)
	{
		
		$array_return[]		=	array("title" => $each->plaintext,"link" => break_url_forum(check_url($site_url, $each->href)));
	}
	unset($html);
	if(count($array_return) == 0) $array_return[0] = array("title" => "", "link" => "");
	return $array_return;	
}

function get_content_data($url, $luat_title = "", $luat_content = ""){
	$url = str_replace("http://","",$url);
	$url = "http://" . $url;
	//echo $url;
	$html 							= get_url_html($url);
	$title							= $html->find($luat_title,0)->plaintext;
	$comment = $html->find($luat_content);
	for($i=0; $i<count($comment); $i++)
	{
		$comment[$i] = clearHTML($comment[$i]->innertext());
	}	
	unset($html);
	unset($content);
	$array_return = array("title" => $title, "desc" => $comment);
	return $array_return;	
}

function check_url($site_url, $sub_url){
	$sub_url = break_url_forum($sub_url);
	if(substr($sub_url,0,7) != "http://"){
		$sub_url = $site_url . $sub_url;
	}
	//$sub_url = str_replace("//","/",$sub_url);
	return $sub_url;
}

function clearHTML($string){
	$string = mb_convert_encoding($string,"UTF-8","UTF-8");
	$string = preg_replace ('/<script.*?\>.*?<\/script>/si', ' ', $string); 
	$string = preg_replace ('/<style.*?\>.*?<\/style>/si', ' ', $string); 
	$string = preg_replace ('/<font.*?\>.*?<\/font>/si', ' ', $string);
	$string = preg_replace ('/<h2.*?\>.*?<\/h2>/si', ' ', $string);  
	$string = preg_replace ('/<a.*?\>/si', '', $string);
	$string = preg_replace ('/<\/a>/si', '', $string);	
	$string = str_replace ('&nbsp;', ' ', $string);
	//$string = html_entity_decode ($string);
	return $string;
}

//function lấy các category
function get_category1($html, $site_name)
{
	$cat_link = array();
	$i = 0;
	switch($site_name){
		case "otofun.net":	
			$container = $html->find("div[class=datacontainer]");
			foreach($container as $each)
			{
				$html = str_get_html($each->outertext);
				$count = count($html->find("div[class=subforums]"));
				if($count == 0)
				{
					$no_sub = $html->find("h2[class=forumtitle] a", 0);
					$cat_link['category'][$i] = $no_sub->plaintext;
					$cat_link['link'][$i] = break_url_forum("http://" . $site_name . "/" . $no_sub->href);
				}else
				{
					$has_sub = $html->find("div[class=subforums] li[class=subforum] a");
					foreach($has_sub as $sub)
					{
						$cat_link['category'][$i] = $sub->plaintext;
						$cat_link['link'][$i] = break_url_forum("http://" . $site_name . "/" . $sub->href);
					}
				}
				$i++;
			}
			break;
			
		case "ddth.com":
			$container = $html->find("td[id*=f] a");
			foreach($container as $each)
			{
				$cat_link['category'][$i] = $each->plaintext;
				$cat_link['link'][$i] = break_url_forum("http://" . $site_name . "/" . $each->href);
				$i++;	
			}
		
		case "thienlong.org":
			$container = $html->find("td[id*=f]");
			foreach($container as $each)
			{
				$html = str_get_html($each->outertext);
				$count = count($html->find("div[class=smallfont]"));
				if($count > 1)
				{
					$big_cat = $html->find("a", 0);
					$sub_cat = $html->find("div[class=smallfont] a");
					foreach($sub_cat as $each_sub_cat)
					{
						$cat_link['category'][$i] = $big_cat->plaintext . " - " . $each_sub_cat->plaintext;
						$cat_link['link'][$i] = break_url_forum("http://" . $site_name . "/forumvn/" . $each_sub_cat->href);
						$i++;
					}
				}else
				{
					$cat_link['category'][$i] = $html->find("a", 0)->plaintext;
					$cat_link['link'][$i] = break_url_forum("http://" . $site_name . "/forumvn/" . $html->find("a", 0)->href);
					$i++;
				}
				
			}
			
		/*case "hoa-viet.com":
			$container = $html->find("td[id*=f]");
			foreach($container as $each)
			{
				$html = str_get_html($each->outertext);
				$count = count($html->find("div[class=smallfont]"));
				if($count > 1)
				{
					$sub_cat = $html->find("div[class=smallfont] td[class=smallfont] a");
					foreach($sub_cat as $each_sub_cat)
					{
						$cat_link['category'][$i] = $each_sub_cat->plaintext;
						$cat_link['link'][$i] = break_url_forum("http://" . $site_name . "/forumvn/" . $each_sub_cat->href);
						$i++;
					}
				}else
				{
					$cat_link['category'][$i] = $html->find("a", 0)->plaintext;
					$cat_link['link'][$i] = break_url_forum("http://" . $site_name . "/forumvn/" . $html->find("a", 0)->href);
					$i++;
				}
			}*/
			
		case "autovina.vn":
			$container = $html->find("td[id*=f]");
			foreach($container as $each)
			{
				$html = str_get_html($each->outertext);
				$count = count($html->find("div[class=smallfont]"));
				if($count > 1)
				{
					$big_cat = $html->find("a", 0);
					$sub_cat = $html->find("div[class=smallfont] a");
					foreach($sub_cat as $each_sub_cat)
					{
						$cat_link['category'][$i] = $big_cat->plaintext . " - " . $each_sub_cat->plaintext;
						$cat_link['link'][$i] = break_url_forum("http://" . $site_name . "/forum/" . $each_sub_cat->href);
						$i++;
					}
				}else
				{
					$cat_link['category'][$i] = $html->find("a", 0)->plaintext;
					$cat_link['link'][$i] = break_url_forum("http://" . $site_name . "/forum/" . $html->find("a", 0)->href);
					$i++;
				}
				
			}
	}
	return $cat_link;
}

/*Function lấy các link con từ link tổng
	1. $html: khối html truyền vào
	2. $type_forum: loại diễn đàn (vi dụ: vBulletin)
	3. $site_name: tên diễn đàn (gồm cả đuôi .com .net ...)
*/
function get_sub_link($html, $site_name)
{
	switch($site_name){
		case "oto-hui.com":
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($link->href), $link->plaintext);  
         	}
			break;
			
		case "oto365.net":
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($link->href), $link->plaintext);  
         	}
			break;
			
		case "2dep.net":
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($link->href), $link->plaintext);  
         	}
			break;
			
		case "diendan.yeutretho.com":
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($link->href), $link->plaintext);  
         	}
			break;
			
		case "diendan.eva.vn":
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($link->href), $link->plaintext);  
         	}
			break;
			
		case "aha.vn": 
			$arrayReturn = array();
			$sublink = $html->find("div.topicName a");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($link->href), $link->plaintext);  
         	}
			break;
			
		case "tinhte.vn": 
			$arrayReturn = array();
			$sublink = $html->find("h3[class=threadtitle] a[class=title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($link->href), $link->plaintext);  
         	}
			break;
			
		case "otoxomnhala.com": 
			$site_name = str_replace("http://","",$site_name);
			$site_name = "http://" . $site_name;
			$site_name = str_replace("http://www.","http://",$site_name) . "/forums/";
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($site_name . $link->href), $link->plaintext);  
         	}
			break;
			
		case "diendandulich.net": 
			$arrayReturn = array();
			$sublink = $html->find("a[id*=tid-link-]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($link->href), $link->plaintext);  
         	}
			break;
			
		case "diendandulich.com": 
			$arrayReturn = array();
			$sublink = $html->find("a[id*=tid-link-]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($link->href), $link->plaintext);  
         	}
			break;
			
		case "diendandulich.net": 
			$arrayReturn = array();
			$sublink = $html->find("a[class=topictitle]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($link->href), $link->plaintext);  
         	}
			break;
			
		case "vietravel247.com": 
			$arrayReturn = array();
			$sublink = $html->find("span[id*=msg_] a");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($link->href), $link->plaintext);  
         	}
			break;
			
		case "ketcau.com": 
			$site_name = str_replace("http://","",$site_name);
			$site_name = "http://" . $site_name;
			$site_name = str_replace("http://www.","http://",$site_name) . "/forum/";
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($site_name . $link->href), $link->plaintext);  
         	}
			break;
            
        case "autovina.vn": 
			$site_name = str_replace("http://","",$site_name);
			$site_name = "http://" . $site_name;
			$site_name = str_replace("http://www.","http://",$site_name) . "/forum/";
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($site_name . $link->href), $link->plaintext);  
         	}
			break;
			
		case "nhatnghe.com": 
			$site_name = str_replace("http://","",$site_name);
			$site_name = "http://" . $site_name;
			$site_name = str_replace("http://www.","http://",$site_name) . "/FORUM/";
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($site_name . $link->href), $link->plaintext);  
         	}
			break;
			
		case "ictvietnam.net": 
			$site_name = str_replace("http://","",$site_name);
			$site_name = "http://" . $site_name;
			$site_name = str_replace("http://www.","http://",$site_name) . "/forum/";
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($site_name . $link->href), $link->plaintext);  
         	}
			break;
			
		case "thienlong.org": 
			$site_name = str_replace("http://","",$site_name);
			$site_name = "http://" . $site_name;
			$site_name = str_replace("http://www.","http://",$site_name) . "/forumvn/";
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($site_name . $link->href), $link->plaintext);  
         	}
			break;
			
		case "forum.bongda24h.vn": 
			$site_name = str_replace("http://","",$site_name);
			$site_name = "http://" . $site_name;
			$site_name = str_replace("http://www.","http://",$site_name);
			$arrayReturn = array();
			$sublink = $html->find("a[class=post_link]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($site_name . $link->href), $link->plaintext);  
         	}
			break;
			
		default: 
			$site_name = str_replace("http://","",$site_name);
			$site_name = "http://" . $site_name;
			$site_name = str_replace("http://www.","http://",$site_name) . "/";
			$arrayReturn = array();
			$sublink = $html->find("a[id*=thread_title]");
			foreach($sublink as $link){
            	$arrayReturn[]  = array(break_url_forum($site_name . $link->href), $link->plaintext);  
         	}
			break;
	}
	return $arrayReturn;
}

function break_url_forum($url){
   $arrURL = explode("?",$url);
   $new_url = $arrURL[0] . "?";

   $string = isset($arrURL[1]) ? $arrURL[1] : "";
   //echo  $string . "<br>";
   $arrURL = explode('&amp;',$string);
   foreach($arrURL as $key=>$value){
      if(strpos($value,"s=") === false && strpos($value,"sis=") === false && strpos($value,"sid=") === false){
        $new_url .= $value;
      }
   }
   if(substr($new_url,-1) == "?") $new_url = substr($new_url,0,-1);
   $new_url = str_replace('//','/',$new_url);
   $new_url = str_replace('//','/',$new_url);
   $new_url = str_replace("http:/","http://",$new_url);
   return $new_url;
}

//function lấy tiêu đề bài post
function get_post_title($html, $site_name)
{
	switch($site_name)
	{ 
		case "oto365.net": 
			$title = $html->find('h2[class="title icon"]', 0);
			$title = $title->plaintext;
			break;
			
		case "tinhte.vn": 
			$title = $html->find('h2[class="title icon"]', 0);
			$title = $title->plaintext;
			break;
			
		case "danmexe.net": 
			$title = $html->find('h2[class="title icon"]', 0);
			$title = $title->plaintext;
			break;
			
		case "aha.vn": 
			$title = $html->find("tr[id=topicTitle] b", 0);
			$title = $title->plaintext;
			$title = str_replace("Chủ đề:", "", $title);
			break;
			
		case "oto-hui.com": 
			$title = $html->find('h2[class="title icon"]', 0);
			$title = $title->plaintext;
			break;
			
		case "otofun.net": 
			$title = $html->find('h2[class="posttitle icon"]', 0);
			$title = $title->plaintext;
			break;
			
		case "diendandulich.com": 
			$title = $html->find('div b', 1);
			$title = $title->plaintext;
			break;
			
		case "2dep.net": 
			$title = $html->find('td[id*=td_post] div.smallfont strong', 0);
			$title = $title->plaintext;
			break;
			
		case "diendanviet.com": 
			$title = $html->find('strong[class=bigusername]', 0);
			$title = $title->plaintext;
			break;
			
		case "diendan.cauduongcang.com": 
			$title = $html->find("h4[class=ForumPostTitle]", 0);
			$title = $title->plaintext;
			break;
			
		case "forum.clip.vn": 
			$title = $html->find('h2[class="posttitle icon"]', 0);
			$title = $title->plaintext;
			break;
			
		case "forum.bongda24h.vn": 
			$title = $html->find('td[class=header1Title] span[id=forum__ctl0_TopicTitle]', 0);
			$title = $title->plaintext;
			break;
            
        case "autovina.vn": 
			$title = $html->find("div.smallfont strong", 1);
			$title = $title->plaintext;
			break;
			
		default: 
			$title = $html->find("div.smallfont strong", 0);
			$title = $title->plaintext;
			break;
	}
	return $title;
}

//function lấy nội dung bài viết và các comment
function get_post_comment($html, $site_name)
{
	switch($site_name)
	{
		case "aha.vn": 
			$comment = $html->find("div.postContent");
			for($i=0; $i<count($comment); $i++)
				{
					$comment[$i] = $comment[$i]->plaintext;
				}	
			break;
			
		case "diendandulich.com": 
			$comment = $html->find("div[class=postcolor]");
			for($i=0; $i<count($comment); $i++)
			{
				$comment[$i] = $comment[$i]->plaintext;
			}
			break;
		
		case "diendan.cauduongcang.com": 
			$comment = $html->find("div[id*=ctl00_ctl01_bcr_ctl00]");
			for($i=0; $i<count($comment); $i++)
				{
					$comment[$i] = $comment[$i]->plaintext;
				}	
			break;
			
		case "forum.bongda24h.vn": 
			$comment = $html->find("td[class=message]");
			for($i=0; $i<count($comment); $i++)
				{
					$comment[$i] = $comment[$i]->plaintext;
				}	
			break;
		
		default: 
			$comment = $html->find("div[id*=post_message]");
			for($i=0; $i<count($comment); $i++)
				{
					$comment[$i] = $comment[$i]->plaintext;
				}	
			break;
	}
	return $comment;
}

//function lấy ngày post comment
function get_post_date($html, $site_name)
{
	switch($site_name)
	{
		case "oto365.net":
			$date = $html->find("span[class=date]");	
			for($i=0; $i<count($date); $i++)
			{
				$date[$i] = $date[$i]->plaintext;
				$date[$i] = trim($date[$i]);
				$date[$i] = substr($date[$i], 0, 10);
			}
			break;

		case "danmexe.net":
			$date = $html->find("span[class=date]");	
			for($i=0; $i<count($date); $i++)
			{
				$date[$i] = $date[$i]->plaintext;
				$date[$i] = trim($date[$i]);
				$date[$i] = substr($date[$i], 0, 10);
			}
			break;
			
		case "aha.vn":
			$date = $html->find("td[align=right]");	
			for($i=0; $i<count($date); $i++)
			{
				$date[$i] = $date[$i]->plaintext;
				$date[$i] = trim($date[$i]);
				$date[$i] = substr($date[$i], strlen($date[$i])-10, strlen($date[$i]));
			}
			break;
			
		case "oto-hui.com":
			$date = $html->find("span[class=date]");	
			for($i=0; $i<count($date); $i++)
			{
				$date[$i] = $date[$i]->plaintext;
				$date[$i] = trim($date[$i]);
				$date[$i] = substr($date[$i], 0, 10);
			}
			break;
			
		case "otofun.net":
			$date = $html->find("span[class=date]");	
			for($i=0; $i<count($date); $i++)
			{
				$date[$i] = $date[$i]->plaintext;
				$date[$i] = trim($date[$i]);
				$date[$i] = substr($date[$i], 0, 10);
			}
			break;	
		
		case "otoxomnhala.com":
			$text = $html->find("td[class=thead]");
			$j=0;
			for($i=0; $i<count($text) - 3; $i++)
				for($x=0; $x<count($text); $x++)
				{
					if($i == 3*$x)
					{
						$date[$j] = $text[$i];
						$j++;
					}
				}
			for($i=0; $i<count($date); $i++)
			{
				$date[$i] = $date[$i]->plaintext;
				$date[$i] = trim($date[$i]);
				$date[$i] = substr($date[$i], 0, 10);
			}
			break;
		
		default:
			$date = array();					
			$text = $html->find("td.thead div.normal");
			$j=0;
			for($i=0; $i<count($text); $i++)
			{
				if($i%2 == 1)
				{
					$date[$j] = $text[$i];
					$j++;
				}
			}
			for($i=0; $i<count($date); $i++)
			{
				$date[$i] = $date[$i]->plaintext;
				$date[$i] = trim($date[$i]);
				$date[$i] = substr($date[$i], 0, 10);
			}
			break;
	}
	
	for($i=0; $i<count($date); $i++)
	{
		$str = "ôm nay";
		$e = strpos($date[$i], $str);
		if($e != false)
		{
			$date[$i] = time("d-m-Y");
		}
		$str = "ôm qua";
		$e = strpos($date[$i], $str);
		if($e != false)
		{
			$date[$i] = time((time("d") - 1) . "-m-Y");
		}
		$date[$i] =  convertDateTime($date[$i]);
	}
	return $date;
}

//function bỏ biến session trong sublink
function remove_session($url)
{
	$start 	= strpos($url, "s=");
	$end	= strpos($url, "&");
	$part1	= substr($url, 0, $start);
	$part2	= substr($url, $end+1);
	$url	= $part1 . $part2;
	return $url;
}

//function lấy số page của số 
function get_page1($html, $site_name)
{
	switch($site_name)
	{
		case "ddth.com":
			$page 	= $html->find("td.vbmenu_control", 0);
			$page 	= trim($page->plaintext);
			$start 	= strpos($page, "/");
			$page 	= substr($page, $start+1);
			intval($page);
			break;
		
	}
	return $page;
}	

//function lấy các title của sublink
/*function get_sub_link_title($html, $site_name)
{
	switch($site_name)
	{
		case "ddth.com": 
			$subtitle = $html->find("a[id*=thread_title]");
			for($i=0; $i<count($subtitle); $i++)
			{
				$subtitle[$i] = $subtitle[$i]->plaintext;
			}
			break;
			
		case "oto365.net": 
			$subtitle = $html->find("a[id*=thread_title]");
			for($i=0; $i<count($subtitle); $i++)
			{
				$subtitle[$i] = $subtitle[$i]->plaintext;
			}
			break;

		case "autovina.vn": 
			$subtitle = $html->find("a[id*=thread_title]");
			for($i=0; $i<count($subtitle); $i++)
			{
				$subtitle[$i] = $subtitle[$i]->plaintext;
			}
			break;	
			
		case "xevietnam.com": 
			$subtitle = $html->find("a[id*=thread_title]");
			for($i=0; $i<count($subtitle); $i++)
			{
				$subtitle[$i] = $subtitle[$i]->plaintext;
			}
			break;
			
		case "thuvienoto.com": 
			$subtitle = $html->find("a[id*=thread_title]");
			for($i=0; $i<count($subtitle); $i++)
			{
				$subtitle[$i] = $subtitle[$i]->plaintext;
			}
			break;
			
		case "danmexe.net": 
			$subtitle = $html->find("a[id*=thread_title]");
			for($i=0; $i<count($subtitle); $i++)
			{
				$subtitle[$i] = $subtitle[$i]->plaintext;
			}
			break;
			
		case "aha.vn": 
			$subtitle = $html->find("div.topicName a");
			for($i=0; $i<count($subtitle); $i++)
			{
				$subtitle[$i] = $subtitle[$i]->plaintext;
			}
			break;
			
		case "oto-hui.com": 
			$subtitle = $html->find("a[id*=thread_title]");
			for($i=0; $i<count($subtitle); $i++)
			{
				$subtitle[$i] = $subtitle[$i]->plaintext;
			}
			break;
			
		case "otofun.net": 
			$subtitle = $html->find("a[id*=thread_title]");
			for($i=0; $i<count($subtitle); $i++)
			{
				$subtitle[$i] = $subtitle[$i]->plaintext;
			}
			break;
			
		case "otoxomnhala.com": 
			$subtitle = $html->find("a[id*=thread_title]");
			for($i=0; $i<count($subtitle); $i++)
			{
				$subtitle[$i] = $subtitle[$i]->plaintext;
			}
			break;
	}
	return $subtitle;
}
*/

?>